package org.alvin;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class DESDemo {


    /**
     * 密钥 我这里随便弄几个
     */
    public static final byte [] DES_KEY = new byte[]{0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};

    public static byte [] genKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 加密
     * @param text
     * @return
     * @throws Exception
     */
    public static String encrypt(String text) throws  Exception {
        DESKeySpec desKeySpec = new DESKeySpec(DES_KEY);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        Key convertSecretKey = factory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Hex.encodeHexString(result);
    }

    /**
     * 解密
     * @param text
     * @return
     * @throws Exception
     */
    public static String decrypt(String text) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec(DES_KEY);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        Key convertSecretKey = factory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        byte [] result = Hex.decodeHex( text.toCharArray());
        result = cipher.doFinal(result);
        return  new String(result);
    }

    public static String sensitiveData(String text,int perfix,int suffix){
        String regex = String.format("(\\d{%d})\\d*(\\d{%d})",perfix,suffix);
        String encodeChar = "*";
        StringBuilder replacement = new StringBuilder("$1");
        for(int i = 0;i <text.length() - perfix - suffix;i++){
            replacement.append(encodeChar);
        }
        replacement.append("$2");
       return text.replaceAll(regex, replacement.toString());
    }

    public static void main(String[] args) throws Exception {
        testPhone("13923440323");
        testIDCard("311120238410231231");
        testIDCard("2311120238410231231x");

        System.out.printf("fe80:0:0:0:216:3eff:fe10:7991%eth0".split("[%]")[0].trim());
    }

    public static void testPhone(String phone) throws Exception {
        //手机号 保存
        System.out.println("原手机号：" + phone);
        //加密存库
        String enPhone =encrypt(phone);
        System.out.println("加密后的手机号：" + enPhone);
        //解密查看
        String rePhone = decrypt(enPhone);
        System.out.println("加密后的手机号：" + rePhone);
        //前端显示脱敏
        String sPhone = sensitiveData(rePhone,3,4);
        System.out.println("脱敏后的手机号:" + sPhone);
        System.out.println("\n\n");
    }

    public static void testIDCard(String idCard) throws Exception {
        //手机号 保存
        System.out.println("原身份证号：" + idCard);
        //加密存库
        String enPhone =encrypt(idCard);
        System.out.println("加密后的身份证号：" + enPhone);
        //解密查看
        String rePhone = decrypt(enPhone);
        System.out.println("加密后的身份证号：" + rePhone);
        //前端显示脱敏
        String sPhone = sensitiveData(rePhone,1,4);
        System.out.println("脱敏后的身份证号:" + sPhone);
        System.out.println("\n\n");
    }

}
